import time

from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
class Base():
    # 初始化
    def __init__(self,driver):
        self.driver=driver
    # 网址链接
    def open_url(self,url):
        self.driver.get(url)
    # 定位
    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,20).until(lambda driver:driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            error_time = time.strftime("%Y%m%d-%H%M%S")
            self.driver.save_screenshot(f"{error_time}.png")
            print(e)

    # 点击
    def click(self,loc):
        self.find_element(loc).click()
    # 输入内容
    def send_keys(self,loc,info):
        self.find_element(loc).send_keys(info)
    # 断言
    def duanyan(self,loc):
        el=self.find_element(loc).text
        return el
    # 切换页面
    def qiehuan(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])
    # 滚动条0，800
    def gundong(self,x):
        self.driver.execute_script(f"window.scrollTo(0,{x})")
    # 弹窗事件
    def alert(self):
        e = self.driver.switch_to.alert
        e.accept()
    # iframe事件
    def iframe(self,loc):
        self.driver.switch_to.frame(self.find_element(loc))
    # 鼠标事件
    def shubiao(self,loc):
        ActionChains(self.driver).double_click(self.find_element(loc)).perform()
    # 键盘事件
    def jianpan(self,loc):
        self.find_element(loc).send_keys(Keys.ENTER)
    # 刷新
    def shuaxin(self):
        self.driver.refresh()
    # 后退
    def book(self):
        self.driver.back()
    # 前进
    def qianjin(self):
        self.driver.forward()
    # 下拉框
    def value(self,loc,info):
        value=Select(self.find_element(loc))
        value.select_by_value(info)
    def index(self,num,loc):
        index=Select(self.find_element(loc))
        index.select_by_index(num)
    def text(self,loc,info):
        text=Select(self.find_element(loc))
        text.select_by_visible_text(info)


